package ws.server.limc.opos
import vaadin.scala._
import scala.util._
import vaadin.scala.server._
import vaadin.scala.mixins.ComponentMixin
//import com.vaadin.{ ui => vu }
//import scala.reflect.ClassTag 
object Validators {

  val nullNotAllow = Validator { v: O[A] =>
    if (v.nonEmpty)
      Validator.Valid
    else Validator.Invalid("不可为空" :: Nil)
  }
  val emptyNotAllow = Validator { value: O[A] =>

    if (value.exists { x => !x.toString().trim().isEmpty() })
      Validator.Valid
    else Validator.Invalid("值需要填写" :: Nil)

  }
  val isBigDec = Validator { value: O[A] =>

    if (value.isEmpty || value.exists { x => x.toString().trim() == "" || Try { BigDecimal(x.toString) }.isSuccess })
      Validator.Valid
    else Validator.Invalid("需要是数字" :: Nil)

  }

  val isInt = Validator { value: O[A] =>

    if (value.isEmpty || value.exists { x => x.toString().trim() == "" || Try { (x.toString).toInt }.isSuccess })
      Validator.Valid
    else Validator.Invalid("需要是数字" :: Nil)

  }

}

object show {
  def apply(an: A) = Notification.show(an.toString())
  def error(other: A) = Notification.show(other.toString(), Notification.Type.Error)
  def tray(an: A) = Notification.show(an.toString(), Notification.Type.Tray)
}

trait PropertyExp[T] extends Property[T] {
  override def value_=(v: A) = super.value_=(v match {
    //    case b: BigDecimal => b.bigDecimal
    case o => o.toString
  })
  def strValue_=(v: A) =
    if (v != null) value = v.toString else ""
  def asStrVal = value.map { _.toString }.getOrElse("")
  def asBigDecVal = value.map { v => BigDecimal(v.toString) }.getOrElse(BigDecimal(0))
  def asIntVal = value.map { v => (v.toString).toInt }.getOrElse(0)
}

object StreamSource {
  def apply(is: => java.io.InputStream) = new com.vaadin.server.StreamResource.StreamSource {
    def getStream = is
  }
}
object StreamResource {
  def apply(sourceFile: com.vaadin.server.StreamResource.StreamSource, filename: S) = new StreamResource(sourceFile, filename)
  def apply(filename: S)(is: => java.io.InputStream) = new StreamResource(StreamSource(is), filename)
}

class StreamResource(override val pResource: com.vaadin.server.StreamResource with vaadin.scala.server.mixins.ResourceMixin) extends Resource {

  def this(sourceFile: com.vaadin.server.StreamResource.StreamSource, filename: S) {
    this(new com.vaadin.server.StreamResource(sourceFile, filename) with vaadin.scala.server.mixins.ResourceMixin)
  }
}

class DragAndDropWrapper(root: Component) extends com.vaadin.ui.DragAndDropWrapper(root.p) with com.vaadin.event.dd.DropHandler {
  //setDropHandler(this)
  class DragAndDropEvent(dropEvent: com.vaadin.event.dd.DragAndDropEvent) {
    val transferable = dropEvent.getTransferable.asInstanceOf[WrapperTransferable]
    def files = transferable.getFiles.filter { null != }
    def text = transferable.getText
  }

  var dropFn: O[(DragAndDropEvent => U)] = None

  def setDropFn(e: DragAndDropEvent => U) =
    dropFn = Some(e)

  def drop(dropEvent: com.vaadin.event.dd.DragAndDropEvent) = {
    if (dropFn.nonEmpty)
      dropFn.get(new DragAndDropEvent(dropEvent))
    /*
     *  final Html5File[] files = tr.getFiles();
36.            if (files != null) {
37.                for (final Html5File html5File : files) {
38.                    final String fileName = html5File.getFileName();
39. 
40.                    if (html5File.getFileSize() > FILE_SIZE_LIMIT) {
41.                        Notification
42.                                .show("File rejected. Max 2Mb files are accepted by Sampler",
43.                                        Notification.Type.WARNING_MESSAGE);
44.                    } else {
45. 
46.                        final ByteArrayOutputStream bas = new ByteArrayOutputStream();
47.                        final StreamVariable streamVariable = new StreamVariable() {
48. 
49.                            @Override
50.                            public OutputStream getOutputStream() {
51.                                return bas;
52.                            }
53. 
54.                            @Override
55.                            public boolean listenProgress() {
56.                                return false;
57.                            }
58. 
59.                            @Override
60.                            public void onProgress(
61.                                    final StreamingProgressEvent event) {
62.                            }
63. 
64.                            @Override
65.                            public void streamingStarted(
66.                                    final StreamingStartEvent event) {
67.                            }
68. 
69.                            @Override
70.                            public void streamingFinished(
71.                                    final StreamingEndEvent event) {
72.                                progress.setVisible(false);
73.                                showFile(fileName, html5File.getType(), bas);
74.                            }
75. 
76.                            @Override
77.                            public void streamingFailed(
78.                                    final StreamingErrorEvent event) {
79.                                progress.setVisible(false);
80.                            }
81. 
82.                            @Override
83.                            public boolean isInterrupted() {
84.                                return false;
85.                            }
86.                        };
87.                        html5File.setStreamVariable(streamVariable);
88.                        progress.setVisible(true);
89.                    }
90.                }
91. 
92.            } else {
93.                final String text = tr.getText();
94.                if (text != null) {
95.                    showText(text);
96.                }
97.            }
     * 
     */
  }

  def getAcceptCriterion = com.vaadin.event.dd.acceptcriteria.AcceptAll.get

}

//class MultiFileUpload(handleFileFn: (java.io.InputStream, S, S, L) => U)//
//(override val p: com.wcs.wcslib.vaadin.widget.multifileupload.ui.MultiFileUpload with ComponentMixin = new com.wcs.wcslib.vaadin.widget.multifileupload.ui.MultiFileUpload(
//  new com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadFinishedHandler {
//    def handleFile(file: java.io.InputStream, name: S, mimeType: S, len: L) = {
//      handleFileFn(file, name, mimeType, len)
//    }
//  }, new com.wcs.wcslib.vaadin.widget.multifileupload.ui.UploadStateWindow) with ComponentMixin) extends Component {
//
//}

trait FixScaladin {
  def t() = {}
}


